/*
 * VHCupMatchesTab.java
 * Created on 2 août 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.tabs;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.VHMainController;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.ui.base.VHTabItem;
import com.hockeo.vhweb.client.ui.panels.VHTabPanelRRCupRound;
import com.hockeo.vhweb.client.ui.widget.VHKOMatchGrid;
import com.hockeo.vhweb.client.ui.widget.VHRoundSelector;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Displays a round selector and the corresponding list of matches.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHCupMatchesTab extends VHTabItem
{
  private String            d_strContestId;
  private VHRoundSelector   d_roundSelector;
  private BorderLayoutData  d_layoutDataCenter;

  // Center widget (three possibilities: no data, KO round, RR round)
  //
  private Widget            d_widgetCurrent;
  private ContentPanel      d_panelNoResult;
  private VHKOMatchGrid     d_matchGrid;
  private VHTabPanelRRCupRound d_panelRRCupRound;

  public VHCupMatchesTab( VHController controller, String strContestId )
  {
    super( controller, VHMain.getConstants().matchList() );

    d_strContestId = strContestId;

    setIcon( VHMain.ICON_MATCH_LIST );
  }

  @Override
  public void buildContent()
  {
    BorderLayoutData data;
    final VHMainController ctr = (VHMainController) getController();

    if ( isContentReady() )
      return;

    setLayoutOnChange( true );

    // Northern settings panel
    //
    ContentPanel panelNorth = new ContentPanel( new RowLayout( Style.Orientation.VERTICAL ) );
    panelNorth.setHeaderVisible( false );

    // round selector
    //
    d_roundSelector = new VHRoundSelector();
    ctr.loadContestRounds( d_roundSelector, d_strContestId );
    panelNorth.add( d_roundSelector, new RowData( Style.DEFAULT, Style.DEFAULT, new Margins( 4, 4, 4, 4 ) ) );

    data = new BorderLayoutData( LayoutRegion.NORTH );
    data.setMargins( new Margins( 4, 4, 4, 4 ) );
    data.setSize( 32f );
    add( panelNorth, data );

    // let's first put a panel that we do not have any results, as soon as a round has
    // been loaded,
    // the panel will be replaced.
    //
    HtmlContainer html = new HtmlContainer( "<div style='margin-top: 15px; text-align: center; width: 100%;'><p>"
        + VHMain.getConstants().noDataForSelectedRound() + "</p><p style='margin-top: 10px;'>"
        + VHMain.getConstants().contestNotOrganisedThisSeason() + "</p></div>" );
    d_widgetCurrent = d_panelNoResult = new ContentPanel( new BorderLayout() );
    d_panelNoResult.add( html, new BorderLayoutData( LayoutRegion.CENTER ) );
    d_panelNoResult.setHeaderVisible( false );

    d_layoutDataCenter = new BorderLayoutData( LayoutRegion.CENTER );
    d_layoutDataCenter.setMargins( new Margins( 0, 4, 4, 4 ) );
    add( d_panelNoResult, d_layoutDataCenter );

    // event listeners
    //
    d_roundSelector.getCombobox().addSelectionChangedListener( new SelectionChangedListener<VHJsData>() {
      @SuppressWarnings( "synthetic-access" )
      @Override
      public void selectionChanged( SelectionChangedEvent<VHJsData> se )
      {
        VHJsData roundSelected = se.getSelectedItem();
        onRoundSelected( roundSelected );
      }
    } );

    super.buildContent();
  }

  /**
   * Automatically called as soon as a round has been selected.
   *
   * @param roundSelected the selected round
   */
  private void onRoundSelected( VHJsData round )
  {
    final VHMainController ctr = (VHMainController) getController();

    if ( round.getString( "format" ).equals( "KNOCK_OUT" ) )
    {
      if ( d_matchGrid == null )
        d_matchGrid = new VHKOMatchGrid( ctr, 3, true );

      ctr.loadKOMatches( d_matchGrid, d_strContestId, round.getId() );

      if ( !d_widgetCurrent.equals( d_matchGrid ) )
      {
        d_widgetCurrent.removeFromParent();
        add( d_matchGrid, d_layoutDataCenter );
        d_widgetCurrent = d_matchGrid;
      }
    }
    else if ( round.getString( "format" ).equals( "ROUND_ROBIN" ) )
    {
      if ( d_panelRRCupRound == null )
        d_panelRRCupRound = new VHTabPanelRRCupRound( getController() );

      ctr.loadContestElements( d_panelRRCupRound, round.getId() );

      if ( !d_widgetCurrent.equals( d_panelRRCupRound ) )
      {
        d_widgetCurrent.removeFromParent();
        add( d_panelRRCupRound, d_layoutDataCenter );
        d_widgetCurrent = d_panelRRCupRound;
      }
    }
    else
      Window.alert( "Unsupported contest round format: " + round.getString( "format" ) );
  }
}
